package com.ainge.easystudy.designpattern.factory.simple;

import com.ainge.easystudy.designpattern.factory.ICourse;

import java.util.Optional;

/**
 * 简单工厂
 *
 * @author: Ainge
 * @Time: 2021/4/24 14:07
 */
public class SimpleFactory {
    private SimpleFactory() {
        throw new RuntimeException("SimpleFactory不需要被实例化");
    }

    /**
     * 创建课程
     * @param cls 需要创建的课程类
     * @return nullable
     */
    public static ICourse create(Class<? extends ICourse> cls) {
        return Optional.ofNullable(cls).map(aClass -> {
            try {
                return aClass.newInstance();
            } catch (Exception e) {
                return null;
            }
        }).orElse(null);
    }

}
